class FurnitureStylesController < ApplicationController
  
  autocomplete :furniture_style,:code, :display_value => :full_name
  
  def index
    params[:key_word] = "%#{params[:key_words]}%"
    @furniture_styles = FurnitureStyle.where('code like :key_word or name like :key_word',params).visible.order('created_at asc')
  end
  
  def new
    @furniture_style = FurnitureStyle.new
  end
  
  def create
    @furniture_style = FurnitureStyle.new(params[:furniture_style])
    if @furniture_style.save
      redirect_to :action => :index
    else
      render :new
    end
  end
  
  def edit
    @furniture_style = FurnitureStyle.find(params[:id])
  end
  
  def update
    @furniture_style = FurnitureStyle.find(params[:id])
    if @furniture_style.update_attributes(params[:furniture_style])
      redirect_to :action => :index
    else
      render :edit
    end
  end
  
  def hide
    @furniture_style = FurnitureStyle.find(params[:id])
    @furniture_style.hidden!
    redirect_to :action => :index
  end

  def visible
    @furniture_style = FurnitureStyle.find(params[:id])
    @furniture_style.visible!
    redirect_to :action => :index
  end
  
  def auto_complete_code
    @codes = FurnitureStyle.where(['code like ? ',"params[:code]%" ]).order('code asc').limit(20).each_with_object([]) do |furniture_style, codes|
      codes << furniture_style.code
    end
  end
end
